import { EditorView } from "@tiptap/pm/view";

import LinterPlugin, { Result as Issue } from "../LinterPlugin";

export class HeadingLevel extends LinterPlugin {
  fixHeader(level: number) {
    return function ({ state, dispatch }: EditorView, issue: Issue) {
      dispatch(state.tr.setNodeMarkup(issue.from - 1, undefined, { level }));
    };
  }

  scan() {
    let lastHeadLevel: number | null = null;

    this.doc.descendants((node, position) => {
      if (node.type.name === "heading") {
        // Check whether heading levels fit under the current level
        const { level } = node.attrs;
        if (lastHeadLevel != null && level > lastHeadLevel + 1) {
          this.record(`这个标题太小了 当前(${level} 上一个是 ${lastHeadLevel})`, position + 1, position + 1 + node.content.size, this.fixHeader(lastHeadLevel + 1));
        }
        lastHeadLevel = level;
      }
    });

    return this;
  }
}
